package com.qijian.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dyvmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dyvmsapi20170525.models.SingleCallByTtsRequest;
import com.aliyun.sdk.service.dyvmsapi20170525.models.SingleCallByTtsResponse;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.qijian.common.annotation.SourceType;
import darabonba.core.client.ClientOverrideConfiguration;
import org.apache.regexp.RE;

import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;


/**
 * @author Alex
 * 拨打语音电话工具类
 */
public class MobileCallUtil {
    // 区域
    private final static String AREA = "cn-hangzhou";
    // ID
    private final static String ACCESS_KEY_ID = "LTAI5tAtyg5LJpVBY85iFnUd";
    // key
    private final static String ACCESS_KEY_SECRET = "ox0LZacmXa9JjSCCS8VOnW27HkuH6w";

    private final static String CALL_SHOW_NUMBER = "057128067694";

    private final static Integer PLAY_TIMES = 3;
    private final static Integer VOLUME = 100;
    private final static Integer SPEED = 3;


    // redis缓存key
    public final static String SMS_PREFIX = "user::sms:";

    public static AsyncClient buildClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ACCESS_KEY_ID)
                .accessKeySecret(ACCESS_KEY_SECRET)
                .build());
        AsyncClient client = AsyncClient.builder()
                .region(AREA)
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dyvmsapi.aliyuncs.com")
                )
                .build();
        return client;
    }

    public static String callMobile(String targetMobile, String jsonStrParam, MobileCallTemplateEnum callType, String outId) {
        String respStr;
        try {
            SingleCallByTtsRequest singleCallByTtsRequest = SingleCallByTtsRequest.builder()
                    .calledNumber(targetMobile)
                    .ttsCode(callType.getTmpId())
                    .calledShowNumber(CALL_SHOW_NUMBER)
                    .playTimes(PLAY_TIMES)
                    .volume(VOLUME)
                    .speed(SPEED)
                    .outId(outId)
                    .ttsParam(jsonStrParam)
                    .build();
            AsyncClient client = buildClient();

            CompletableFuture<SingleCallByTtsResponse> response = client.singleCallByTts(singleCallByTtsRequest);
            SingleCallByTtsResponse resp = response.get();

            respStr = new Gson().toJson(resp);
            JSONObject body = JSON.parseObject(respStr).getJSONObject("body");

            respStr = "OK".equals(body.getString("code")) ? "OK" + body : "ERR " + body;
            // Finally, close the client
            client.close();

        } catch (Exception e) {
            respStr = "ERR" + e.getCause() + "\n" + e.getMessage() + "\n" + e.getStackTrace();
        }
        return respStr;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(callMobile("17601403633", "{\"name\":\"徐荣伟\",\"carNos\":[\"苏Es8ys22\",\"苏M12345\"]}", MobileCallTemplateEnum.WLFYD, "23"));
    }

    public enum MobileCallTemplateEnum {
        //物流发运单
        // 模板参数:name,carNos
        /**
         * 物流发运单
         * 模板参数:name,carNos
         * 案例 {\"name\":\"学习\",\"carNos\":[\"苏Eess22\",\"苏M12345\"]}"
         */
        WLFYD("TTS_285175038"),
        /**
         * 竞价提醒
         * 案例 {\"name\":\"学习\",\"title\":\"采购竞价\",\"number\":\"XSJJ231022\"}"
         */
        JJTX("TTS_287045074");


        /**
         * 名称
         */
        private String tmpId;


        public static String getTmpIdByType(String type) {
            for (MobileCallTemplateEnum e : MobileCallTemplateEnum.values()) {
                if (e.toString().equals(type)) {
                    return e.tmpId;
                }
            }
            return "";
        }

        private MobileCallTemplateEnum(String tmpId) {
            this.tmpId = tmpId;
        }

        public String getTmpId() {
            return tmpId;
        }

        public void setTmpId(String tmpId) {
            this.tmpId = tmpId;
        }
    }

}
